/**
 * Created by midori on 2015/12/14.
 */
(function(){
    var module = angular.module("directives.Wizard",[
        'restangular',
        "services.TestService",
        "services.utils.RestAngularHandler",
        "services.Utils"
    ]);

    module.directive("acWizard", ['TestService',
        'Restangular',
        'RestAngularHandlerFactory',
        "$http",
        "$templateCache",
        "$compile",
        "utils",

        function(testService, restangular, restAngularHandlerFactory, $http, $templateCache, $compile, utils){
            return {
                restrict: "A",
                replace: false,
                transclude: false,
                templateUrl: "/app/templates/wizard/wizard",
                scope: {
                    tabs: "=acWizardParameters",
                    submit: "=acSubmitMethod"
                },
                link: function postLink(scope, elem, attr) {
                    scope.currentIndex = 0;
                    scope.tabNums = scope.tabs.length;
                    scope.values = [];
                    scope.select = function(index){
                        scope.currentIndex = index;
                        // set the active li to not active
                        elem.find('.nav-pills li.active').attr({"class" : ""});
                        // set the target li to active
                        elem.find('#li' + index).attr({"class" : "active"});
                        // set the active tab-pane to not active
                        elem.find('.tab-pane.active').attr({"class" : "tab-pane"});
                        // set the target tab-pane to active
                        elem.find('#tab' + index).attr({"class" : "tab-pane active"});
                        // update the progress bar
                        var percent = (scope.currentIndex + 1)/scope.tabNums * 100;
                        elem.find('.bar').css({width: percent + '%'});
                    };

                    scope.init = function(){
                        for(var i = 0; i < scope.tabs.length;i ++){
                            for(var j = 0; j < scope.tabs[i].fields.length;j ++){
                                var field = scope.tabs[i].fields[j];
                                if(field.options != null){
                                    var promise = field.options;
                                    field.options = [];
                                    (function(field){
                                        promise.then(function(resultArray){
                                            for(var i = 0;i < resultArray.length;i++){
                                                field.options.unshift(resultArray[i]);
                                            }
                                        });
                                    })(field);
                                }
                            }
                        }
                    };

                    scope.submitByArray = function(){
                        var parameters = [];
                        for(var i = 0; i < scope.tabs.length;i ++){
                            for(var j = 0; j < scope.tabs[i].fields.length;j ++){
                                var field = scope.tabs[i].fields[j];
                                if(field.value != null){
                                    parameters.push(field.value);
                                }
                            }
                        }
                        scope.submit.apply(this, parameters);
                    };

                    scope.init();
                }
            }
        }]);


})(this);